package com.sky.service.impl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartImpl implements ShoppingCartService
{
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetMealMapper setMealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO)
    {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //先拿到用户的id
        /**
         * TODO 这里有个BUG：userId一直是Null
         */
        BaseContext.setCurrentId(5L);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //判断当前购物车的商品是否存在
        List<ShoppingCart> list= shoppingCartMapper.list(shoppingCart);
        //存在则数量+1
        if( !list.equals(null) && list.size() > 0 )
        {
            ShoppingCart cart=list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        }
        else//如果不存在，则需要插入一条购物车数据
        {

            Long dishId=shoppingCartDTO.getDishId();
            //判断本次添加到购物车的是菜品还是套餐,因为要查询不同表
            if(dishId != null)//套餐是没有dishId的
            {
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            else//是套餐
            {
                Long setMealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setMealMapper.getById(setMealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 根据用户id查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart()
    {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> res = shoppingCartMapper.list(shoppingCart);
        return res;
    }

    @Override
    public void clean()
    {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO)
    {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //先判断这个商品是 "菜品" 还是 "套餐"
        if(shoppingCart.getSetmealId() != null)//是套餐
        {
            Setmeal setmeal = setMealMapper.getById(list.get(0).getSetmealId());
            //如果有且数量>1：数量-1
            if(list != null && list.get(0).getNumber()> 1)
            {
                BeanUtils.copyProperties(list.get(0),shoppingCart);
                shoppingCart.setNumber(list.get(0).getNumber()-1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
            //如果数量=1，删除这条记录
            else if(list != null && list.get(0).getNumber() == 1)
            {
                shoppingCartMapper.sub(setmeal.getName());
            }
        }
        else//是菜品
        {
            Dish dish = dishMapper.getById(list.get(0).getDishId());
            //如果有且数量>1：数量-1
            if(list != null && list.get(0).getNumber()> 1)
            {
                BeanUtils.copyProperties(list.get(0),shoppingCart);
                shoppingCart.setNumber(list.get(0).getNumber()-1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
            //如果数量=1，删除这条记录
            else if(list != null && list.get(0).getNumber() == 1)
            {
                shoppingCartMapper.sub(dish.getName());
            }
        }
    }
}
